package org.vectrics.recruiter.event;

import java.util.Iterator;

import org.apache.log4j.Logger;
import org.vectrics.common.db.ChangedPropertiesSet;
import org.vectrics.common.db.ChangedProperty;
import org.vectrics.common.db.EventGenerator;
import org.vectrics.event.Event;
import org.vectrics.event.EventType;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.job.JobStatus;
import org.vectrics.recruiter.user.Interview;

public class RecruiterEventGenerator implements EventGenerator {
	private transient Logger log = Logger.getLogger(this.getClass());
	
	public RecruiterEventGenerator() {
		super();
	}

	public void handleModifiedEntity(Object entity, ChangedPropertiesSet changedProperties) {
		if (ThreadProperties.getEventsOff())
			return;
		
		if (log.isDebugEnabled()) {
			log.debug("handleModifiedEntity() entity class=" + entity.getClass().getName());
			log.debug("handleModifiedEntity() entity=" + entity);
			log.debug("handleModifiedEntity() ChangedProperties=" + changedProperties.toString());
		}
		if (entity instanceof Job) {
			createJobModifiedEvent((Job)entity, changedProperties);
		} else if (entity instanceof JobCandidate) {
			createJobCandidateEvent((JobCandidate)entity);
		} else if (entity instanceof Interview) {
			createInterviewEvent((Interview)entity, false);
		}
	}

	
	public void handleCreatedEntity(Object entity) {
		if (log.isDebugEnabled()) {
			log.debug("handleCreateddEntity() entity class=" + entity.getClass().getName());
			log.debug("handleCreatedEvent for entity=" + entity.toString());
		}

		if (ThreadProperties.getEventsOff())
			return;
		
		
		if (entity instanceof JobStatus) {
			createJobStatusModifiedEvent((JobStatus)entity);
		} else if (entity instanceof JobCandidate) {
			createJobCandidateEvent((JobCandidate)entity);
		} else if (entity instanceof Interview) {
			createInterviewEvent((Interview)entity, true);
		}
	}

	public void handleRemovedEntity(Object entity) {
		if (ThreadProperties.getEventsOff())
			return;
		

	}

	
	public void createJobCandidateEvent(JobCandidate jobCandidate) {
		Event changeEvent = new Event();
		changeEvent.setDomain(jobCandidate.getJob().getDomain());
		changeEvent.setDescription("Job candidate created");
		EventType statusChangeEventType 
				= CoreServiceLocator.getEventService().findEventType(JobCandidate.EVENT_CREATED);
		changeEvent.setEventType(statusChangeEventType);
		changeEvent.addAttachment(jobCandidate, JobCandidate.OBJECT_TYPE, 
				"JobCandidate created. ID = " + jobCandidate.getId());
		//CoreServiceLocator.getEventService().updateEvent(changeEvent);
		ThreadProperties.getEvents().add(changeEvent);
	}

	public void createInterviewEvent(Interview interview, boolean created) {
		Event changeEvent = new Event();
		changeEvent.setDomain(interview.getDomain());
		if (created) {
			changeEvent.setDescription("Interview created");
		} else {
			changeEvent.setDescription("Interview modified");
		}
		
		EventType statusChangeEventType 
			= CoreServiceLocator.getEventService().findEventType("interview.sched");
		changeEvent.setEventType(statusChangeEventType);
		changeEvent.addAttachment(interview, Interview.OBJECT_TYPE, 
				"Interview created/modified. ID = " + interview.getId());
		//CoreServiceLocator.getEventService().updateEvent(changeEvent);
		ThreadProperties.getEvents().add(changeEvent);
	}

	
	public void createJobModifiedEvent(Job job, ChangedPropertiesSet changedProperties) {
		Iterator iter = changedProperties.iterator();
		while (iter.hasNext()) {
			ChangedProperty changedProperty = (ChangedProperty)iter.next();
			log.debug("Chg Job Event: " + changedProperty.getName() + "  New value: " + changedProperty.getNewValue());
		}
		
		log.debug("Making event for Job status changed");
		Event changeEvent = new Event();
		changeEvent.setDomain(job.getCompany().getDomain());
		changeEvent.setDescription("Job order changed");
		EventType statusChangeEventType 
				= CoreServiceLocator.getEventService().findEventType(Job.EVENT_CHANGE);
		changeEvent.setEventType(statusChangeEventType);
		changeEvent.addAttachment(job, Job.OBJECT_TYPE, 
				"Job that changed status. ID = " + job.getId());
		ThreadProperties.getEvents().add(changeEvent);
	}

	public void createJobStatusModifiedEvent(JobStatus jobStatus) {
		log.debug("Making event for Job status changed");
		Event changeEvent = new Event();
		changeEvent.setDomain(jobStatus.getJob().getCompany().getDomain());
		changeEvent.setDescription("Job status changed");
		EventType statusChangeEventType 
				= CoreServiceLocator.getEventService().findEventType(Job.EVENT_STATUS_CHANGE);
		changeEvent.setEventType(statusChangeEventType);
		changeEvent.addAttachment(jobStatus.getJob(), Job.OBJECT_TYPE, 
				"Job that changed status. ID = " + jobStatus.getJob().getId());
		ThreadProperties.getEvents().add(changeEvent);
	}
	
}
